package io.yunho.yunho.interview.two.threadlocal.ref;

import java.io.IOException;

/**
 * @author: Jason
 * @date: 2021/4/27 17:35
 * @description:
 */
public class NormalReferDemo {
    static class NormalReferTest{
        protected void finalize() throws Throwable{
            System.out.println("对象被回收了");
        }
    }
    public static void main(String[] args) {
        NormalReferTest normalReferTest = new NormalReferTest();
        System.out.println("开始，第一次执行垃圾回收");
        System.gc();
        try {
            Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
        normalReferTest = null;//给垃圾回收线程执行,正常引用设置为Null就会被垃圾回收器回收finalize
        System.out.println("开始第二次GC，此时引用被设置为null");
        System.gc();
        try {
            Thread.sleep(500);} catch (InterruptedException e) {e.printStackTrace();}
        try {
            System.in.read();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
